package org.apache.jsp.ForumHtml;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import exception.NotLoggedInException;
import java.util.LinkedList;
import java.rmi.Naming;
import server.ForumHandlerInt;
import server.NotificationMsg;
import java.util.Vector;
import java.rmi.Naming;
import server.ForumHandlerInt;

public final class notifier_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {


public static int FRIEND_ADDED_POST = 0;
public static int CREATORS_TOPIC_UPDATED = 1;
public static int NEW_POST_IN_CURRENT_TOPIC = 2;
public static int FRIEND_UPDATED_POST = 3;
public static int POST_UPDATED_IN_CURRENT_TOPIC = 4;
public static int POST_DELETED_IN_CURRENT_TOPIC = 7;
public static int FRIEND_DELETED_POST = 8;


  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.List<java.lang.String> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.ArrayList<java.lang.String>(1);
    _jspx_dependants.add("/ForumHtml/serverConnection.jsp");
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.List<java.lang.String> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=windows-1255");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("    \r\n");
      out.write("    \r\n");
      out.write("<NOTIFICATIONS>\r\n");
      out.write("\r\n");
      out.write("\r\n");

	
   String serverDomain = "localhost";
   ForumHandlerInt forum = null;
  
     try
     {
   	 forum =  (ForumHandlerInt)Naming.lookup("rmi://"+serverDomain+"/ForumService");
	   }
   catch (Exception e){
	   
	   
	   response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server.");
	   response.flushBuffer();
		return;
   }

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");


LinkedList<NotificationMsg> inbox = null;
try
{
	inbox = (forum.getNotificationMsgsNb((String)session.getAttribute("userName")));
	
	for (NotificationMsg nmsg:inbox)
	{
		//String reason = null;
		//switch (nmsg.getType())
		//{
		//	case 0 : reason = "Your friend added new post"; break;
		//	case 1 : reason = "New post added to one of your topics"; break;
		//	case 2 : reason = "New post has added to the topic that you are looking at"; break;
		//}
		
      out.write(" \r\n");
      out.write("\t\t<MSG>\r\n");
      out.write("\t\t   <REASON>");
      out.print(nmsg.getType());
      out.write("</REASON> \r\n");
      out.write("\t\t   <USER>");
      out.print(nmsg.getUserName());
      out.write("</USER>\r\n");
      out.write("\t\t   <CREATOR>");
      out.print(nmsg.getPost().getCreator().getUserName());
      out.write("</CREATOR>\r\n");
      out.write("\t\t   <FORUMID>");
      out.print(nmsg.getForum().getId());
      out.write("</FORUMID>\r\n");
      out.write("\t\t   <FORUM>");
      out.print(nmsg.getForum().getName());
      out.write("</FORUM>\r\n");
      out.write("\t\t   <TOPICID>");
      out.print(nmsg.getTopic().getId());
      out.write("</TOPICID>\r\n");
      out.write("\t\t   <TOPIC>");
      out.print(nmsg.getTopic().getTitle());
      out.write("</TOPIC>\r\n");
      out.write("\t\t   <POST>");
      out.print(nmsg.getPost().getTitle() );
      out.write("</POST>\r\n");
      out.write("\t\t   <POSTID>");
      out.print(nmsg.getPost().getId() );
      out.write("</POSTID>\r\n");
      out.write("\t\t   <POSTCONTENT>");
      out.print(nmsg.getPost().getContent() );
      out.write("</POSTCONTENT>\r\n");
      out.write("\t\t   <CREATION>");
      out.print(nmsg.getPost().getCreationTime().toLocaleString() );
      out.write("</CREATION>\r\n");
      out.write("\t\t   <EDITING>");
      out.print(nmsg.getPost().getEditingTime().toLocaleString() );
      out.write("</EDITING>\r\n");
      out.write("\t\t</MSG>\r\n");
      out.write("\t\t\r\n");
      out.write("\t\t");

	}
	

}
catch(NotLoggedInException e)
{
	response.sendRedirect("loggin.jsp");
	response.flushBuffer();
	return;
}
catch (Exception e)
{
  response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server.");
  response.flushBuffer();
  return;
}



      out.write(" \r\n");
      out.write("\r\n");
      out.write("</NOTIFICATIONS>\r\n");
      out.write("\r\n");
      out.write("\r\n");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
